/*
 * Created on Jun 26, 2011
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.timber.util;

import java.sql.CallableStatement;
import java.sql.Statement;
import java.sql.Types;

/**
 * @author joju.varghese
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SalesOrderManager {
	  public static String insertSalesOrder(BillBean iteamBean)
	  {
	    SLProcDescriptor procParams = new SLProcDescriptor("common..p_InsSaleOrder");
	    try {
	    	procParams.addProcP(iteamBean.getItemId());
	    	procParams.addProcP(iteamBean.getInvoiceNumber()+"");  //invoice number
	    	procParams.addProcP(iteamBean.getTimberName());
	    	procParams.addProcP(iteamBean.getTClass());
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getSalesRate()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getLenght()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getWidth()));
	    	procParams.addProcP(iteamBean.getMesurement());
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getSQuantity()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getUnits()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getSalesRate()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getSalesRate()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getTax()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getTotal()));
	    	procParams.addProcP(Global.roundThreeDecimals(iteamBean.getAmount()));
	    	procParams.addProcP(Global.getComapnayName());
	    	String sqlStr = procParams.getProcName() + procParams.getProcParams();
	    	Statement st = Global.getDBConnection().createStatement();
	    	Util.log( "insertSalesOrder.updateCustomer()", sqlStr );
	    	System.out.println("[Item ] started insert ");
	    	st.execute( sqlStr );
	    	System.out.println("[Item ] end insert ");
	    	st.close();
	    }
	    catch( Exception ex )
		{
	    	System.out.println("[Item ] cannot insert ");
		}
	    return "Succes";
	  }
	  public static String deleteSalesOrder(BillBean iteamBean)
	  {
	    SLProcDescriptor procParams = new SLProcDescriptor("common..p_DelSaleOrder");
	    try {
	    	procParams.addProcP(iteamBean.getItemId());
	    	procParams.addProcP(iteamBean.getInvoiceNumber()+"");  //invoice number
	    	procParams.addProcP(Global.getComapnayName());
	    	String sqlStr = procParams.getProcName() + procParams.getProcParams();
	    	Statement st = Global.getDBConnection().createStatement();
	    	Util.log( "deleteSalesOrder.deleteSalesOrder()", sqlStr );
	    	System.out.println("[Item ] started insert ");
	    	st.execute( sqlStr );
	    	System.out.println("[deleteSalesOrder ] end delete ");
	    	st.close();
	    }
	    catch( Exception ex )
		{
	    	System.out.println("[deleteSalesOrder ] cannot delete ");
		}
	    return "Succes";
	  }
	  
	  
	  public static int getInvoiceNumber()
	  {
	  	int nextId = -1;
	  	String sproc = "{call common..p_GetNextIdentity( ?,?,?,?,? )}";
	  	
	  	try
		{
	  		CallableStatement cstmt = Global.getDBConnection().prepareCall( sproc );
	  		cstmt.setString( 1, "common" );
	  		cstmt.setString( 2, "t_SalesOrder" );
	  		cstmt.setInt(    3, 1 );
	  		cstmt.setString( 4, Global.getComapnayName() );
	  		cstmt.registerOutParameter( 5, Types.INTEGER );
	  		cstmt.executeUpdate();
	  		nextId = cstmt.getInt( 5 );
	  		cstmt.close();
		}
	  	catch( Exception ex )
		{
	  		Util.error( "DataCacheMgr.getNextIntercoGroupId(): " +
	  				"Caught exception while trying to get next intercompany group ID - " +
					ex.getMessage() );
		}
	  	
	  	return nextId;
	  }
	  
	  
	  public static String insertPayment(String invoiceNumber,String customerName ,double OpenAmount,double PaymentAmount  )
	  {
	    SLProcDescriptor procParams = new SLProcDescriptor("common..p_InsPayment");
	    try {
	    	procParams.addProcP(invoiceNumber);  //invoice number
	    	procParams.addProcP(customerName);
	    	procParams.addProcP(new Double(OpenAmount));
	    	procParams.addProcP(new Double(PaymentAmount));
	    	procParams.addProcP(Global.getComapnayName());
	    	String sqlStr = procParams.getProcName() + procParams.getProcParams();
	    	Statement st = Global.getDBConnection().createStatement();
	    	Util.log( "insert Payment()", sqlStr );
	    	System.out.println("[Payment ] started insert ");
	    	st.execute( sqlStr );
	    	System.out.println("[Payment ] end insert ");
	    	st.close();
	    }
	    catch( Exception ex )
		{
	    	System.out.println("[Payment ] cannot insert ");
		}
	    return "Succes";
	  }
	  

}
